package com.file.parser;

import com.file.bo.AppGjzwfwGjj;
import com.file.bo.ChromeGjjAnyBaseInfo;
import com.file.bo.ResponseData;
import com.file.constant.ErrorCode;
import com.file.util.JsonUtils;
import lombok.extern.slf4j.Slf4j;

import java.text.DecimalFormat;


@Slf4j
public class ChromeGjjAnyToAppGjzwfwGjjConverter {
    public static ResponseData<String> convert(String baseInfoJson) {
        String json = null;
        try {
            ChromeGjjAnyBaseInfo baseInfo = JsonUtils.convertJsonToObject(baseInfoJson, ChromeGjjAnyBaseInfo.class);
            ChromeGjjAnyBaseInfo.PersonalInfo basePersonalInfo = baseInfo.getPersonalInfo();
            ChromeGjjAnyBaseInfo.DepositInfo baseDepositInfo = baseInfo.getDepositInfo();
            ChromeGjjAnyBaseInfo.CenterInfo baseCenterInfo = baseInfo.getCenterInfo();

            AppGjzwfwGjj appGjzwfwGjj = new AppGjzwfwGjj();
            AppGjzwfwGjj.PersonalInfo personalInfo = new AppGjzwfwGjj.PersonalInfo();
            personalInfo.setName(basePersonalInfo.getName());
            personalInfo.setPhoneNumber(basePersonalInfo.getPhoneNumber());
            personalInfo.setPersonalAccount(basePersonalInfo.getPersonalAccount());
            personalInfo.setPersonalAccountStatus(basePersonalInfo.getPersonalAccountStatus());
            personalInfo.setPersonalAccountBalance(convertToFormattedAmount(basePersonalInfo.getPersonalAccountBalance()));

            AppGjzwfwGjj.CenterInfo centerInfo = new AppGjzwfwGjj.CenterInfo();
            centerInfo.setCenterNumber(baseCenterInfo.getCenterNumber());
            centerInfo.setCenterName(baseCenterInfo.getCenterName());
            centerInfo.setCompanyName(baseCenterInfo.getCompanyName());

            AppGjzwfwGjj.DepositInfo depositInfo = new AppGjzwfwGjj.DepositInfo();
            depositInfo.setPersonalDepositRatio(convertToFormattedPercentage(baseDepositInfo.getPersonalDepositRatio()));
            depositInfo.setCompanyDepositRatio(convertToFormattedPercentage(baseDepositInfo.getCompanyDepositRatio()));
            depositInfo.setPersonalDepositBase(convertToFormattedAmount(baseDepositInfo.getPersonalDepositBase()));
            depositInfo.setPersonalMonthlyDeposit(convertToFormattedAmount(baseDepositInfo.getPersonalMonthlyDeposit()));
            depositInfo.setCompanyMonthlyDeposit(convertToFormattedAmount(baseDepositInfo.getCompanyMonthlyDeposit()));

            appGjzwfwGjj.setPersonalInfo(personalInfo);
            appGjzwfwGjj.setCenterInfo(centerInfo);
            appGjzwfwGjj.setDepositInfo(depositInfo);
            json = JsonUtils.convertObjectToJson(appGjzwfwGjj);

        } catch (Exception e) {
            log.error("OnError|{}||{}|{}|{}|{}", ErrorCode.FILE_PARSE_EXCEPTION.getCode(),"", "", "", "convert failed", e);
            return new ResponseData<>(json, ErrorCode.FILE_PARSE_EXCEPTION.getCode(), ErrorCode.FILE_PARSE_EXCEPTION.getMsg());
        }

        log.info("convert completed, json:{}", json);
        return new ResponseData<>(json, ErrorCode.SUCCESS.getCode(), ErrorCode.SUCCESS.getMsg());
    }
    public static String convertToFormattedAmount(String amount) {
        if (amount == null || amount.isEmpty()) {
            throw new IllegalArgumentException("Amount cannot be null or empty");
        }
        return amount.replace(",", "").replace("元", "").trim();
    }

    public static String convertToFormattedPercentage(String percentage) {
        if (percentage == null || percentage.isEmpty()) {
            throw new IllegalArgumentException("Percentage cannot be null or empty");
        }

        String cleanedPercentage = percentage.replace("%", "").trim();

        try {
            double value = Double.parseDouble(cleanedPercentage);
            DecimalFormat df = new DecimalFormat("#.00");
            return df.format(value) + "%";
        } catch (NumberFormatException e) {
            throw new IllegalArgumentException("Invalid percentage format: " + percentage, e);
        }
    }

    public static void main(String[] args) {
        String baseInfoJson = "{\"个人信息\":{\"姓名\":\"张三\",\"证件类型\":\"身份证\",\"证件号码\":\"120111111111111111\",\"手机号码\":\"17611111111\",\"个人账号\":\"GJJ11111111\",\"个人账户状态\":\"正常\",\"个人账户余额\":\"560.00元\"},\"缴存信息\":{\"个人缴存基数\":\"2,750.00元\",\"单位缴存比例\":\"10%\",\"个人缴存比例\":\"10%\",\"单位月缴存额\":\"275.00元\",\"个人月缴存额\":\"275.00元\"},\"中心信息\":{\"中心名称\":\"北京住房公积金管理中心\",\"中心编号\":\"110000000000000\",\"单位名称\":\"上海XXXX公司\"}}";
        System.out.println(convert(baseInfoJson));

    }

}
